package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.EnvironmentMapper;
import com.village.zpark.pojo.Environment;
import com.village.zpark.service.EnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class EnvironmentServiceImpl implements EnvironmentService {
    @Autowired
    private EnvironmentMapper environmentMapper;

    @Override
    public List<Environment> findAll() {
        return environmentMapper.selectAll();
    }

    @Override
    public Environment findById(Integer id) {
        return environmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addEnvironment(Environment environment) {
        return environmentMapper.insert(environment);
    }

    @Override
    public int updateEnvironment(Environment environment) {
        return environmentMapper.updateByPrimaryKey(environment);
    }

    @Override
    public int deleteEnvironmentById(Integer id) {
        return environmentMapper.deleteByPrimaryKey(id);
    }


    @Override
    public PageInfo<Environment> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Environment.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("monitor", "%" + param + "%");// 使用 orLike 来实现多字段模糊查询
        }
        List<Environment> environments;
        if (param != null && !param.isEmpty()) {
            environments = environmentMapper.selectByExample(example);
        } else {
            environments = environmentMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(environments);

    }
}




